package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.adapter.ShipmentInquiryBoxAdapter;
import com.mflex.WHSmartWhouseAd.model.CommitBOXLinkPlateModel;
import com.mflex.WHSmartWhouseAd.model.ShipmentInquiryPdaInfo;
import com.mflex.WHSmartWhouseAd.model.ShipmentInquiryPdaResult;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.ArrayList;
import java.util.List;

//import butterknife.BindView;
//import butterknife.ButterKnife;


public class ShippingInquiryActivity extends BaseActivity implements AdapterView.OnItemClickListener  {
  // BPM出门证编号
    EditText stvBPMnumber;
    // 出货编号
    EditText stvShipmentNumber;
    // 列表
    ListView lvList;

    // loading
    MiniLoadingDialog miniLoadingDialog;
    List<ShipmentInquiryPdaInfo>ShipmentModelList=new ArrayList<>();
    List<ShipmentInquiryPdaResult>Rows=new ArrayList<>();
    CommitBOXLinkPlateModel model = new CommitBOXLinkPlateModel();
    private String userName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_shipping_inquiry);
//        ButterKnife.bind(this);
        //        id绑定
        stvBPMnumber = findViewById(R.id.stv_BPMnumber);
        stvShipmentNumber = findViewById(R.id.stv_ShipmentNumber);
        lvList = findViewById(R.id.lvList);
        // 弹窗提示
        miniLoadingDialog =WidgetUtils.getMiniLoadingDialog(this,"加载...");
        // 传递数据
        Intent intent = getIntent();
        userName = intent.getStringExtra("userName");
        // 视图按键监听器
        stvShipmentNumber.setOnKeyListener(this);
        stvShipmentNumber.requestFocus();
        lvList.setOnItemClickListener(this);

        // 界面跳转
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ShippingInquiryActivity.this,SecondaryConfirmOfShipmentActivity.class);
                intent.putExtra("ShipNo", stvShipmentNumber.getText().toString());
                intent.putExtra("BPMno", stvBPMnumber.getText().toString());
                intent.putExtra("CountQty",String.valueOf(Rows.get(position).getCountQty()));
                intent.putExtra("MaterialNo",Rows.get(position).getMaterialNo());
                intent.putExtra("userName", userName);
                startActivity(intent);
            }
        });
    }
    @Override
    public void onClick(View view) {

    }
    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_ShipmentNumber:// 出货编号
                    chekShipmentNumber();
                    break;
                default:
                    break;
            }
        }
        return false;
    }
// 出货编号回车事件触发接口
    private void chekShipmentNumber() {
        if(StringUtils.isEmpty(stvShipmentNumber.getText().toString())) {
            return;
        }
//       测试
//        ShipmentInquiryPdaResult items = new ShipmentInquiryPdaResult();
//        items.setMaterialNo("123");
//        items.setCountQty("100");
//        stvBPMnumber.setText("bpm");
//        List dataList = new ArrayList<>();
//        dataList.add(items);
//        Rows = dataList;
//        ShipmentInquiryBoxAdapter ShipmentInquiryBoxAdapter = new ShipmentInquiryBoxAdapter(ShippingInquiryActivity.this,Rows);
//        lvList.setAdapter(ShipmentInquiryBoxAdapter);
//        结束
     miniLoadingDialog.show();
     XHttp.get("api/WHSmartWhousePDA/FindShipNumber")
             .params("ShipNo",stvShipmentNumber.getText().toString())
             .syncRequest(false)
             .onMainThread(true)
             .execute(new SimpleCallBack<ShipmentInquiryPdaInfo>() {
                 @Override
                 public  void  onSuccess(ShipmentInquiryPdaInfo response) {
                     miniLoadingDialog.dismiss();
                     stvBPMnumber.setText(response.getBPMNo());
                     ShipmentModelList.add(response);
                     Rows = response.getRows();
                     Log.d("response",response.toString());
                     ShipmentInquiryBoxAdapter ShipmentInquiryBoxAdapter = new ShipmentInquiryBoxAdapter(ShippingInquiryActivity.this,Rows);
                     lvList.setAdapter(ShipmentInquiryBoxAdapter);

                 }
                 @Override
                 public void onError(ApiException e) {
                     miniLoadingDialog.dismiss();
                     MediaPlayer mp = MediaPlayer.create(ShippingInquiryActivity.this,R.raw.error_voice);mp.start();
                     new MaterialDialog.Builder(ShippingInquiryActivity.this)
                             .iconRes(R.drawable.icon_warning)
                             .title(R.string.tip_warning)
                             .content(e.getMessage())
                             .positiveText(R.string.lab_submit)
                             .show();
                 }
             });
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}